package br.com.seuimovelnamao.controller;

import java.security.NoSuchAlgorithmException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import br.com.seuimovelnamao.PropertiesLoaderImpl;
import br.com.seuimovelnamao.dao.UsuarioDAO;
import br.com.seuimovelnamao.model.Usuario;

@Controller
public class LoginController {
	
	/**
	 * @return
	 * Exibe a tela de login
	 */
	@RequestMapping("/login")
	public String formLogin(HttpServletRequest request){
		HttpSession session = request.getSession();
		if(session.getAttribute("loginError") != null){
			request.setAttribute("loginError", session.getAttribute("loginError"));
			session.removeAttribute("loginError");
		}
		return "login";
	}
	
	/**
	 * @return
	 * Exibe a tela quando usuario esta logado
	 */
	@RequestMapping("/memberArea")
	public ModelAndView memberArea(){
		
		ModelAndView mv = new ModelAndView("member-area");
		return mv;
	}
	
	/**
	 * @param usuario
	 * @param request
	 * @return
	 * @throws HibernateException
	 * @throws NoSuchAlgorithmException
	 * Faz o login do usuario - senha SEM criptografia
	 */
	@RequestMapping("/efetuaLogin")
	public String efetuarLogin(Usuario usuario, HttpServletRequest request) throws HibernateException, NoSuchAlgorithmException{
		
		Session session = (Session)request.getAttribute("hibernateSession");
		
		HttpSession sessao = request.getSession();
		
		UsuarioDAO dao = new UsuarioDAO(session);
		
		// define lat = 0, lng = 0 para nao dar pau
		usuario.setLat(0);
		usuario.setLng(0);
		
		if(dao.usuarioExiste(usuario)){
			usuario = dao.getUsuarioPorEmail(usuario);
			sessao.setAttribute("usuario", usuario);
			
			return "redirect:memberArea";
		}else{
			request.getSession().setAttribute("loginError", PropertiesLoaderImpl.getValor("erro_login"));
			return "redirect:login";
		}
	}
	
	/**
	 * @param usuario
	 * @param request
	 * @return
	 * @throws HibernateException
	 * @throws NoSuchAlgorithmException
	 * Faz o login do usuario - senha COM criptografia
	 */
	@RequestMapping("/efetuaLoginSenhaCriptografada")
	public String efetuarLoginSenhaCriptografada(Usuario usuario, HttpServletRequest request) throws HibernateException, NoSuchAlgorithmException{
		
		Session session = (Session)request.getAttribute("hibernateSession");
		
		HttpSession sessao = request.getSession();
		
		UsuarioDAO dao = new UsuarioDAO(session);
		
		// define lat = 0, lng = 0 para nao dar pau
		usuario.setLat(0);
		usuario.setLng(0);
		
		if(dao.usuarioExisteSenhaCriptografada(usuario)){
			sessao.setAttribute("usuario", usuario);
			
			return "redirect:memberArea";
		}else{
			request.getSession().setAttribute("loginError", PropertiesLoaderImpl.getValor("erro_login"));
			return "redirect:login";
		}
			
	}
	
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request){
		request.getSession().invalidate();
		return "redirect:/";
	}

}
